local hx__chengxiang = fk.CreateSkill{
  name = "hx__chengxiang",
}

Fk:addPoxiMethod{
  name = "hx__chengxiang_count",
  card_filter = function(to_select, selected, data)
    if table.contains(data[2], to_select) then return true end
    local n = Fk:getCardById(to_select).number
    for _, id in ipairs(data[2]) do
      n = n + Fk:getCardById(id).number
    end
    return n < 14
  end,
  feasible = Util.TrueFunc,
}

local spec = {
  on_use = function(self, event, target, player, data)
    local room = player.room
    local y = player.maxHp
    local cards = room:getNCards(y)
    room:turnOverCardsFromDrawPile(player, cards, hx__chengxiang.name)
    local get = room:askToArrangeCards(player, {
      skill_name = hx__chengxiang.name,
      card_map = {cards},
      prompt = "#hx__chengxiang-choose",
      free_arrange = false,
      box_size = 0,
      max_limit = {13, 13},
      min_limit = {0, 1},
      poxi_type = "hx__chengxiang_count",
      default_choice = {{}, {cards[1]}},
    })[2]
    room:moveCardTo(get, Player.Hand, player, fk.ReasonJustMove, hx__chengxiang.name, nil, true, player)
    room:cleanProcessingArea(cards, hx__chengxiang.name)
    local n = 0
    for _, id in ipairs(get) do
      n = n + Fk:getCardById(id).number
    end
    if n == 13 and player.maxHp ~= 13 then
      room:addPlayerMark(player, "hx__chengxiang13")
    end
  end,
}

hx__chengxiang:addEffect(fk.Damaged, {
  anim_type = "masochism",
  trigger_times = function(self, event, target, player, data)
    return data.damage
  end,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__chengxiang.name)
  end,
  on_use = spec.on_use,
})

hx__chengxiang:addEffect(fk.EventPhaseStart, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(hx__chengxiang.name) and player.phase == Player.Judge
  end,
  on_use = spec.on_use,
})


return hx__chengxiang